<?php
declare (strict_types = 1);

namespace app\common\libs\upload;

use app\admin\service\ConfigService;
use app\common\model\AttachmentModel;

/**
 * 本地上传
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class Local extends UploadDriver
{

    private $siteurl;

    public function __construct($config)
    {
        if($config['siteurl'] == '' || $config['siteurl'] == '/'){
            $this->siteurl = request()->domain();
        }else{
            $this->siteurl = $config['siteurl'];
        }
    }

    /**
     * 本地上传
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @param AttachmentModel $attachment
     * @return object
     */
    public function upload(AttachmentModel $attachment){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile($attachment->module, $file);
        $url = \think\facade\Filesystem::disk('public')->getConfig()->get('url');
        //域名
        $siteurl = $this->siteurl;
        $attachment->filepath = $savename;
        $attachment->fileurl = $siteurl.$url.'/'.$savename;
        return $attachment;
    }
}
